#include "GainSettingView.h"
#include "ui_GainSettingForm.h"
#include "SettingHelper.h"
#include <QTextCodec>


GainSettingView::GainSettingView(QWidget *parent) : QWidget(parent),
   ui(new Ui::GainSetting)
{
   ui->setupUi(this);
   this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);//隐藏标题设置为顶层显示
   InitTables();
}

GainSettingView::~GainSettingView()
{
    delete ui;
}

void GainSettingView::closeEvent(QCloseEvent *e)
{
    saveSettings();
}

void GainSettingView::InitTables()
{
    const QString gainSetRow1[2]={tr(""),tr("增益系数(%)")};
    const QString gainSetCol1[4]={tr("LAS"), tr("MAS"), tr("WAS"),tr("宽度")};

    QString stylesheettableWidget_listing = "QTableWidget{background-color:#ffffff;"
                                            "alternate-background-color: #e2f7eb;"
                                            "color: rgb(0, 0, 0);"
                                            "border:1px solid#c9c9c9"
                                            "selection-color: rgb(255, 255, 255);"
                                            "selection-background-color: rgb(32, 131, 95);}"
                                            ;

    tc = QTextCodec::codecForName("UTF-8");
    ui->tableWidget_gainSet1->setColumnCount(2);
    ui->tableWidget_gainSet1->setRowCount(5);
    ui->tableWidget_gainSet1->setEditTriggers(QAbstractItemView::AllEditTriggers );//编辑
    //ui->tableWidget_gainSet1->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    //ui->tableWidget_gainSet1->verticalHeader()->setResizeMode(QHeaderView::Stretch);
    ui->tableWidget_gainSet1->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    ui->tableWidget_gainSet1->verticalHeader()->setResizeMode(QHeaderView::Stretch);
    //ui->tableWidget_gainSet1->setSelectionMode(QAbstractItemView::NoSelection ); //选择
    ui->tableWidget_gainSet1->setSelectionMode(QAbstractItemView::SingleSelection); //选择
    ui->tableWidget_gainSet1->setSelectionBehavior(QAbstractItemView::SelectItems);
    ui->tableWidget_gainSet1->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);
    ui->tableWidget_gainSet1->verticalHeader()->hide();
    ui->tableWidget_gainSet1->horizontalHeader()->hide();

    for(int i = 0;i<2;++i)
    {
        ui->tableWidget_gainSet1->setItem(0,i,new QTableWidgetItem(gainSetRow1[i]));
        ui->tableWidget_gainSet1->item(0,i)->setFlags( Qt::NoItemFlags );
        //ui->tableWidget_gainSet1->item(0,i)->setFosave_paramnt( QFont( "Times", 10, QFont::Bold ) );  //将字体加粗
        ui->tableWidget_gainSet1->item(0,i)->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget_gainSet1->item(0,i)->setBackgroundColor(QColor(102, 204, 153));
        ui->tableWidget_gainSet1->item(0,i)->setTextColor(QColor(0, 0, 0));
    }
    for(int i = 1;i<5;++i)
    {
        ui->tableWidget_gainSet1->setItem(i,0, new QTableWidgetItem(gainSetCol1[i-1]));
        ui->tableWidget_gainSet1->item(i,0)->setFlags( Qt::NoItemFlags );
        ui->tableWidget_gainSet1->item(i,0)->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget_gainSet1->item(i,0)->setBackgroundColor(QColor(102, 204, 153));
        ui->tableWidget_gainSet1->item(i,0)->setTextColor(QColor(0, 0, 0));

        ui->tableWidget_gainSet1->setItem( i, 1, new QTableWidgetItem( "" ));
        ui->tableWidget_gainSet1->item( i, 1)->setTextAlignment(Qt::AlignCenter);
    }
    ui->tableWidget_gainSet1->setAlternatingRowColors(true);//间隔显示颜色

    ui->tableWidget_gainSet1->setStyleSheet(stylesheettableWidget_listing);

    //2
    const QString gainSetRow2[3]={"", tr("设定值"), tr("本底电压")};
    const  QString gainSetCol2[2]={"RBC", "HGB"};
    ui->tableWidget_gainSet2->setColumnCount(3);
    ui->tableWidget_gainSet2->setRowCount(3);
    ui->tableWidget_gainSet2->setEditTriggers(QAbstractItemView::AllEditTriggers);//编辑

    ui->tableWidget_gainSet2->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    ui->tableWidget_gainSet2->verticalHeader()->setResizeMode(QHeaderView::Stretch);
    //ui->tableWidget_gainSet2->setSelectionMode(QAbstractItemView::NoSelection); //选择

    ui->tableWidget_gainSet2->setSelectionMode(QAbstractItemView::SingleSelection); //选择
    ui->tableWidget_gainSet2->setSelectionBehavior(QAbstractItemView::SelectItems);
    ui->tableWidget_gainSet2->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);
    ui->tableWidget_gainSet2->verticalHeader()->hide();
    ui->tableWidget_gainSet2->horizontalHeader()->hide();

    for(int i = 0;i<3;++i)
    {
        ui->tableWidget_gainSet2->setItem(0,i,new QTableWidgetItem(gainSetRow2[i]));
        ui->tableWidget_gainSet2->item(0,i)->setFlags( Qt::NoItemFlags );
        ui->tableWidget_gainSet2->item(0,i)->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget_gainSet2->item(0,i)->setBackgroundColor(QColor(102, 204, 153));
        ui->tableWidget_gainSet2->item(0,i)->setTextColor(QColor(0, 0, 0));

    }
    for(int i = 1;i<3;++i)
    {
        ui->tableWidget_gainSet2->setItem(i,0, new QTableWidgetItem(gainSetCol2[i-1]));
        ui->tableWidget_gainSet2->item(i,0)->setFlags( Qt::NoItemFlags );
        ui->tableWidget_gainSet2->item(i,0)->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget_gainSet2->item(i,0)->setBackgroundColor(QColor(102, 204, 153));
        ui->tableWidget_gainSet2->item(i,0)->setTextColor(QColor(0, 0, 0));
        ui->tableWidget_gainSet2->setItem( i, 1, new QTableWidgetItem( "" ));
        ui->tableWidget_gainSet2->item( i, 1)->setTextAlignment(Qt::AlignCenter);

        ui->tableWidget_gainSet2->setItem( i, 2, new QTableWidgetItem( "" ));
        ui->tableWidget_gainSet2->item(i, 2)->setFlags( Qt::ItemIsSelectable );
        ui->tableWidget_gainSet2->item(i, 2)->setFlags( Qt::ItemIsEditable );
        ui->tableWidget_gainSet2->item(i, 2)->setTextAlignment(Qt::AlignCenter);
    }
    ui->tableWidget_gainSet2->setAlternatingRowColors(true);//间隔显示颜色
    ui->tableWidget_gainSet2->setStyleSheet(stylesheettableWidget_listing);

    //声明一个委托
    gaDelegate = new LineEditDelegate(this);

    QString conditions = "(^[0]|(^[1-9]\\d{2}))$";
    gaDelegate->getlineeditconditions(conditions);
    ui->tableWidget_gainSet1->setItemDelegateForColumn(1,gaDelegate);
    ui->tableWidget_gainSet2->setItemDelegateForColumn(1,gaDelegate);
    QRegExp regx("(^[0]|(^[1-9]\\d{2}))$");
    QValidator *validator = new QRegExpValidator(regx, ui->lineEdit_XanticlockwiseToRBC );
    ui->lineEdit_XanticlockwiseToRBC->setValidator( validator );
    QValidator *validator1 = new QRegExpValidator(regx, ui->lineEdit_XclockwiseToRBC );
    ui->lineEdit_XclockwiseToRBC->setValidator( validator1 );
    QValidator *validator2 = new QRegExpValidator(regx, ui->lineEdit_XSTART );
    ui->lineEdit_XSTART->setValidator( validator2 );
    QValidator *validator3 = new QRegExpValidator(regx, ui->lineEdit_XWBC );
    ui->lineEdit_XWBC->setValidator( validator3 );
    QValidator *validator4 = new QRegExpValidator(regx, ui->lineEdit_YtoStart );
    ui->lineEdit_YtoStart->setValidator( validator4 );

}

void GainSettingView::saveSettings()
{
   SettingHelper * settingHelper = SettingHelper::getSettingHelper();

   // Todo
   settingHelper->gainWriteSetting();
}

